﻿using System;
using System.Diagnostics;
using System.Windows.Input;

namespace personalplaner.common
{
	/// <summary>
	/// This class implements a disposable WaitCursor to 
	/// show an hourglass while some long-running event occurs.
	/// </summary>
	/// <example>
	/// <![CDATA[
	/// 
	/// using (new WaitCursor())
	/// {
	///    .. Do work here ..
	/// }
	/// 
	/// ]]>
	/// </example>
	public class WaitCursor : IDisposable
	{
		#region Data
		private readonly Cursor oldCursor;
		private Stopwatch stopWatch = new Stopwatch();
#if DEBUG
		private string NamespaceAndMethode = "";
#endif
		#endregion

		#region Ctor
		/// <summary>
		/// Constructor
		/// </summary>
		public WaitCursor()
		{
			oldCursor = Mouse.OverrideCursor;
			Mouse.OverrideCursor = Cursors.Wait;
			stopWatch.Start();

#if DEBUG
			System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame(1, true);
			System.Reflection.MethodBase mb = sf.GetMethod();
			string snamespace = ((System.Type)(((System.Reflection.MemberInfo)(mb)).DeclaringType)).FullName;
			string methode = mb.Name;

			this.NamespaceAndMethode = snamespace + "." + methode;
#endif
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Returns the cursor to the default state.
		/// </summary>
		public void Dispose()
		{
			Mouse.OverrideCursor = oldCursor;
#if DEBUG
			Trace.WriteLine(this.NamespaceAndMethode + " took " + stopWatch.ElapsedMilliseconds.ToString("0,0") + " Milliseconds");
#endif
		}
		#endregion
	}
}
